import QtQuick
import QtQuick.Controls
import QtQuick.Controls.Basic
import QtQuick.Layouts
import QtQuick.Particles
import QtQuick.Dialogs
import GoQt

ApplicationWindow {
    id: win
    width: 800; height: 850
    visible: true
    title: "Qt Quick 围棋"
    // 使用木制背景颜色
    color: "#DEB887"  // 木制颜色

    // 游戏状态：0-开始界面，1-游戏界面
    property int gameState: 0
    // 游戏模式：0-本地对战，1-人机对弈
    property int gameMode: 0

    GoBoard { id: go }

    // 结束游戏弹窗
    Dialog {
        id: endGameDialog
        title: "游戏结束"
        modal: true
        anchors.centerIn: parent
        width: 300
        height: 200

        background: Rectangle {
            color: "#DEB887"  // 木制颜色
            border.color: "#8B4513"
            border.width: 2
            radius: 10
        }

        contentItem: Column {
            spacing: 20
            anchors.centerIn: parent

            Text {
                id: endGameResultText
                text: ""
                font.pixelSize: 24
                font.bold: true
                color: "#8B4513"
                anchors.horizontalCenter: parent.horizontalCenter
            }

            Button {
                text: "确定"
                font.pixelSize: 18
                width: 120
                height: 40
                anchors.horizontalCenter: parent.horizontalCenter
                background: Rectangle {
                    color: "#8B4513"
                    radius: 20
                }
                contentItem: Text {
                    text: "确定"
                    color: "#FFFFFF"
                    font.pixelSize: 18
                    font.bold: true
                    horizontalAlignment: Text.AlignHCenter
                    verticalAlignment: Text.AlignVCenter
                }
                onClicked: endGameDialog.close()
            }
        }
    }

    menuBar: MenuBar {
        id: menuBar
        background: Rectangle {
            color: "#D2B48C"  // 木制颜色
            border.color: "#8B4513"  // 深棕色
            border.width: 1
        }
        
    }

    // 开始界面
    Rectangle {
        id: startScreen
        anchors.fill: parent
        color: "transparent"
        visible: gameState === 0

        // 木制风格背景框
        Rectangle {
            anchors.centerIn: parent
            width: 400
            height: 500
            color: "#DEB887"  // 木制颜色
            border.color: "#8B4513"  // 深棕色
            border.width: 2
            radius: 15
            opacity: 0.9
        }

        Column {
            anchors.centerIn: parent
            spacing: 50

            Text {
                text: "围棋"
                font.pixelSize: 48
                font.bold: true
                color: "#8B4513"  // 深棕色
                anchors.horizontalCenter: parent.horizontalCenter
            }

            Column {
                spacing: 20
                anchors.horizontalCenter: parent.horizontalCenter

                Button {
                    text: "本地对战"
                    font.pixelSize: 24
                    width: 200
                    height: 50
                    background: Rectangle {
                        color: "#8B4513"  // 深棕色
                        radius: 25
                    }
                    contentItem: Text {
                        text: "本地对战"
                        color: "#FFFFFF"  // 白色文字
                        font.pixelSize: 24
                        font.bold: true
                        horizontalAlignment: Text.AlignHCenter
                        verticalAlignment: Text.AlignVCenter
                    }
                    onClicked: {
                        gameMode = 0
                        go.setGameMode(0)
                        go.reset()
                        gameState = 1
                    }
                    
                    // 悬停效果
                    scale: ma.pressed ? 0.95 : ma.containsMouse ? 1.05 : 1.0
                    Behavior on scale {
                        NumberAnimation { duration: 150; easing.type: Easing.OutBack }
                    }
                    MouseArea {
                        id: ma
                        anchors.fill: parent
                        hoverEnabled: true
                        onClicked: parent.clicked()
                    }
                }

                Button {
                    text: "人机对弈"
                    font.pixelSize: 24
                    width: 200
                    height: 50
                    background: Rectangle {
                        color: "#A0522D"  // 棕褐色
                        radius: 25
                    }
                    contentItem: Text {
                        text: "人机对弈"
                        color: "#FFFFFF"  // 白色文字
                        font.pixelSize: 24
                        font.bold: true
                        horizontalAlignment: Text.AlignHCenter
                        verticalAlignment: Text.AlignVCenter
                    }
                    onClicked: {
                        gameMode = 1
                        go.setGameMode(1)
                        go.reset()
                        gameState = 1
                    }
                    
                    // 悬停效果
                    scale: ma2.pressed ? 0.95 : ma2.containsMouse ? 1.05 : 1.0
                    Behavior on scale {
                        NumberAnimation { duration: 150; easing.type: Easing.OutBack }
                    }
                    MouseArea {
                        id: ma2
                        anchors.fill: parent
                        hoverEnabled: true
                        onClicked: parent.clicked()
                    }
                }
            }
        }
    }

    // 游戏界面
    Item {
        id: gameScreen
        anchors.fill: parent
        visible: gameState === 1

        // 木制纹理背景
        Image {
            anchors.fill: parent
            source: "qrc:/GoQt/images/wood_texture.svg"  // 木纹图片路径
            fillMode: Image.Tile
            opacity: 0.8
        }

        // 文件操作按钮区域
        Row {
            id: fileButtons
            anchors.left: parent.left
            anchors.top: parent.top
            anchors.margins: 10
            spacing: 10
            
            Button {
                text: "新开"
                font.pixelSize: 14
                width: 80
                height: 30
                background: Rectangle {
                    color: "#8B4513"  // 深棕色
                    radius: 15
                }
                contentItem: Text {
                    text: "新开"
                    color: "#FFFFFF"  // 白色文字
                    font.pixelSize: 14
                    font.bold: true
                    horizontalAlignment: Text.AlignHCenter
                    verticalAlignment: Text.AlignVCenter
                }
                onClicked: {
                    go.reset()
                    // 重置游戏状态
                    if (gameMode === 1) {
                        // 人机对弈模式下的重置逻辑（占位符）
                        go.setGameMode(1)
                    } else {
                        go.setGameMode(0)
                    }
                }
                
                // 悬停效果
                scale: ma3.pressed ? 0.95 : ma3.containsMouse ? 1.05 : 1.0
                Behavior on scale {
                    NumberAnimation { duration: 150; easing.type: Easing.OutBack }
                }
                MouseArea {
                    id: ma3
                    anchors.fill: parent
                    hoverEnabled: true
                    onClicked: parent.clicked()
                }
            }
            
            Button {
                text: "悔棋"
                font.pixelSize: 14
                width: 80
                height: 30
                background: Rectangle {
                    color: "#8B4513"  // 深棕色
                    radius: 15
                }
                contentItem: Text {
                    text: "悔棋"
                    color: "#FFFFFF"  // 白色文字
                    font.pixelSize: 14
                    font.bold: true
                    horizontalAlignment: Text.AlignHCenter
                    verticalAlignment: Text.AlignVCenter
                }
                onClicked: go.undo()
                
                // 悬停效果
                scale: ma6.pressed ? 0.95 : ma6.containsMouse ? 1.05 : 1.0
                Behavior on scale {
                    NumberAnimation { duration: 150; easing.type: Easing.OutBack }
                }
                MouseArea {
                    id: ma6
                    anchors.fill: parent
                    hoverEnabled: true
                    onClicked: parent.clicked()
                }
            }
            
            Button {
                text: "结束点目"
                font.pixelSize: 14
                width: 80
                height: 30
                background: Rectangle {
                    color: "#A0522D"  // 棕褐色
                    radius: 15
                }
                contentItem: Text {
                    text: "结束点目"
                    color: "#FFFFFF"  // 白色文字
                    font.pixelSize: 14
                    font.bold: true
                    horizontalAlignment: Text.AlignHCenter
                    verticalAlignment: Text.AlignVCenter
                }
                onClicked: {
                    var result = go.finalScore()
                    console.log("结束点目结果: " + result)
                    endGameResultText.text = result
                    endGameDialog.open()
                }
                
                // 悬停效果
                scale: ma7.pressed ? 0.95 : ma7.containsMouse ? 1.05 : 1.0
                Behavior on scale {
                    NumberAnimation { duration: 150; easing.type: Easing.OutBack }
                }
                MouseArea {
                    id: ma7
                    anchors.fill: parent
                    hoverEnabled: true
                    onClicked: parent.clicked()
                }
            }
            
            Button {
                text: "返回开始"
                font.pixelSize: 14
                width: 80
                height: 30
                background: Rectangle {
                    color: "#A0522D"  // 棕褐色
                    radius: 15
                }
                contentItem: Text {
                    text: "返回开始"
                    color: "#FFFFFF"  // 白色文字
                    font.pixelSize: 14
                    font.bold: true
                    horizontalAlignment: Text.AlignHCenter
                    verticalAlignment: Text.AlignVCenter
                }
                onClicked: gameState = 0
                
                // 悬停效果
                scale: ma4.pressed ? 0.95 : ma4.containsMouse ? 1.05 : 1.0
                Behavior on scale {
                    NumberAnimation { duration: 150; easing.type: Easing.OutBack }
                }
                MouseArea {
                    id: ma4
                    anchors.fill: parent
                    hoverEnabled: true
                    onClicked: parent.clicked()
                }
            }
            
            Button {
                text: "退出"
                font.pixelSize: 14
                width: 80
                height: 30
                background: Rectangle {
                    color: "#5D4037"  // 深棕褐色
                    radius: 15
                }
                contentItem: Text {
                    text: "退出"
                    color: "#FFFFFF"  // 白色文字
                    font.pixelSize: 14
                    font.bold: true
                    horizontalAlignment: Text.AlignHCenter
                    verticalAlignment: Text.AlignVCenter
                }
                onClicked: Qt.quit()
                
                // 悬停效果
                scale: ma5.pressed ? 0.95 : ma5.containsMouse ? 1.05 : 1.0
                Behavior on scale {
                    NumberAnimation { duration: 150; easing.type: Easing.OutBack }
                }
                MouseArea {
                    id: ma5
                    anchors.fill: parent
                    hoverEnabled: true
                    onClicked: parent.clicked()
                }
            }
        }

        // 添加标题
        Text {
            id: titleText
            text: "围棋"
            font.pixelSize: 28
            font.bold: true
            color: "#8B4513"  // 深棕色
            anchors.horizontalCenter: parent.horizontalCenter
            anchors.top: fileButtons.bottom
            anchors.topMargin: 10
        }

        // 形势显示
        Rectangle {
            id: situationBox
            width: 220
            height: 34
            radius: 8
            color: "#F5DEB3" // 浅木色
            border.color: "#8B4513"
            border.width: 1
            anchors.right: parent.right
            anchors.rightMargin: 12
            anchors.top: fileButtons.bottom
            anchors.topMargin: 10

            Text {
                id: situationText
                text: go.score()
                color: "#8B4513"
                font.pixelSize: 14
                font.bold: true
                anchors.centerIn: parent
                elide: Text.ElideRight
            }

            // 随局面变化刷新（落子/悔棋/重置都会触发boardChanged）
            Connections {
                target: go
                function onBoardChanged() {
                    situationText.text = go.score()
                }
                function onCapturedChanged() {
                    situationText.text = go.score()
                }
            }
        }

        // 回合指示器
        Rectangle {
            id: turnIndicator
            width: 120
            height: 40
            radius: 20
            color: go.isBlackTurn ? "#000000" : "#ffffff"
            border.color: "#8B4513"  // 深棕色
            border.width: 2
            anchors.horizontalCenter: parent.horizontalCenter
            anchors.top: titleText.bottom
            anchors.topMargin: 10
            
            Text {
                id: turnIndicatorText
                text: go.isBlackTurn ? "黑子回合" : "白子回合"
                color: go.isBlackTurn ? "#8B4513" : "#8B4513"  // 深棕色文字
                font.pixelSize: 16
                font.bold: true
                anchors.centerIn: parent
            }
            
            // 绑定到围棋逻辑的回合变化信号
            Connections {
                target: go
                function onTurnChanged() {
                    turnIndicator.color = go.isBlackTurn ? "#000000" : "#ffffff"
                    turnIndicatorText.color = "#8B4513"  // 始终使用深棕色文字
                    turnIndicatorText.text = go.isBlackTurn ? "黑子回合" : "白子回合"
                }
            }
        }

        // 提子计数显示
        Row {
            id: captureCounter
            spacing: 30
            anchors.horizontalCenter: parent.horizontalCenter
            anchors.top: turnIndicator.bottom
            anchors.topMargin: 10
            
            Rectangle {
                width: 80
                height: 30
                color: "#000000"
                radius: 15
                border.color: "#8B4513"  // 深棕色
                border.width: 1
                
                Text {
                    id: blackCaptureText
                    text: "黑子提子: " + go.blackCaptures
                    color: "#8B4513"  // 深棕色文字
                    font.pixelSize: 14
                    font.bold: true
                    anchors.centerIn: parent
                }
            }
            
            Rectangle {
                width: 80
                height: 30
                color: "#ffffff"
                radius: 15
                border.color: "#8B4513"  // 深棕色
                border.width: 1
                
                Text {
                    id: whiteCaptureText
                    text: "白子提子: " + go.whiteCaptures
                    color: "#8B4513"  // 深棕色文字
                    font.pixelSize: 14
                    font.bold: true
                    anchors.centerIn: parent
                }
            }
            
            // 绑定到提子变化信号
            Connections {
                target: go
                function onCapturedChanged() {
                    blackCaptureText.text = "黑子提子: " + go.blackCaptures
                    whiteCaptureText.text = "白子提子: " + go.whiteCaptures
                }
            }
        }

        Board {
            id: board
            go: go  // 将main.qml中的GoBoard实例传递给Board组件
            anchors.horizontalCenter: parent.horizontalCenter
            anchors.top: captureCounter.bottom
            anchors.topMargin: 20
            anchors.bottom: parent.bottom
            anchors.bottomMargin: 20
        }
    }
}
